#!/bin/sh

set +x

if test ! -e scripts/go-test ; then
	echo 'E: Cannot find scripts/go-test. Abort.'
	exit 1
fi

if test ! -d e2e ; then
	echo 'E: Cannot find e2e directory. Abort.'
	exit 1
fi

os=`uname -s`
if [ ! -z "$E2E_PARALLEL" ]; then
    procs=$E2E_PARALLEL
elif [ "$os" = "Linux" ]; then
    procs=`nproc --all`
else
    procs=`getconf _NPROCESSORS_ONLN`
fi

if [ -n "$ALL_PROXY" -o -n "$HTTP_PROXY" -o -n "$HTTPS_PROXY" ]; then
    no_proxy_default="127.0.0.1,localhost,::1"
    if [ -z "$NO_PROXY" ]; then
        NO_PROXY=$no_proxy_default
    else
        NO_PROXY="$no_proxy_default,$NO_PROXY"
    fi
fi

uid=$(id -u)
gid=$(id -g)
id_vars="E2E_ORIG_UID=$uid E2E_ORIG_GID=$gid"
proxy_vars="HTTP_PROXY=$HTTP_PROXY HTTPS_PROXY=$HTTPS_PROXY ALL_PROXY=$ALL_PROXY NO_PROXY=$NO_PROXY"
cred_vars="E2E_DOCKER_USERNAME=$E2E_DOCKER_USERNAME E2E_DOCKER_PASSWORD=$E2E_DOCKER_PASSWORD"
rootless_vars="XDG_RUNTIME_DIR=$XDG_RUNTIME_DIR DBUS_SESSION_BUS_ADDRESS=$DBUS_SESSION_BUS_ADDRESS"
export sudo_args="env -i PATH=$PATH HOME=$HOME $id_vars $proxy_vars $cred_vars $rootless_vars SINGULARITY_E2E_COVERAGE=$SINGULARITY_E2E_COVERAGE"
exec scripts/go-test -sudo -parallel $procs -tags "e2e_test" "$@" ./e2e
